#pragma once

#include "common.h"
#include "color/truecolor.h"
#include "ui/ui.h"
#include "ui/control_base.h"
#include "ui/panel.h"
#include "ui/sliders.h"
#include "ui/bar.h"

namespace anip
{
	class Texture2D;

	namespace ui
	{
		class ColorPickerBase : public Control
		{
		protected:
			TrueColor _color;

		public:
			using Control::Control;

			std::function<void(ColorPickerBase& self)> onColorChanged;

			TrueColor getColor() const { return _color; }
			virtual void setColor(const TrueColor& c) = 0;

			bool enabled = true;
		};

		// my guess is that you don't need an alpha channel slider in a color
		// picker since that is not *really* a part of the 'color' ... ?
		class RectangularColorPicker : public ColorPickerBase
		{
		private:
			const TrivariateColorProvider* _provider = nullptr;
			Draggable<Panel>* _panel = nullptr;
			Button* _preview = nullptr;
			Slider
				* _s1 = nullptr,
				* _s2 = nullptr,
				* _s3 = nullptr;

			Texture2D* _tex = nullptr;
			float _tex_v1 = INFINITY;
			bool _showRange = true;

			Slider* _makeSlider(const TrivariateColorProvider::ComponentInfo& info);
			void _updateTexture();
			void _updateControls(bool onlyPreview = false);
			void _handleSelection(vec2 pos);

		public:
			RectangularColorPicker(Container* parent = nullptr,
				invec2 pos = { 0, 0 }, invec2 size = { 0, 0 },
				const TrivariateColorProvider* provider = TrivariateColorProvider::CIELAB,
				const TrueColor& col = TrueColor(50, 0, 0));

			void setColor(const TrueColor& c) override;
			void paint() override;
			void flow(Flow& f) override;
		};
	}
}